﻿using Cocbook_DAL;
using Cocbook_DTO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Cocbook_checker
{
    public partial class frmSearch : Form
    {
        public frmSearch()
        {
            InitializeComponent();
            LoaddgvBookSearch();
            dgvBookSearch.DataSource = lsb;
            dgvBookSearch.Columns[0].Width = 100;
            dgvBookSearch.Columns[1].Width = 390;
            dgvBookSearch.Columns[2].Width = 100;
            dgvBookSearch.Columns[3].Width = 90;
        }

        public List<SBook> lsb;
        public List<Book> lb;
        public Project project;

        private void txtBookTitle_TextChanged(object sender, EventArgs e)
        {
            if (txtBookTitle.Text == "")
            {
                LoaddgvBookSearch();
            }
            else
            {
                BookDAL pbDAL = new BookDAL();
                lb = new List<Book>();
                lb = pbDAL.GetAllBookByTitle(txtBookTitle.Text);
                ConvertBook_SBook(lb);
                dgvBookSearch.DataSource = lsb;
            }
        }

        public void LoaddgvBookSearch()
        {
            ProjectDAL p = new ProjectDAL();
            project = p.GetProjectByStatus();
            BookDAL boDAL = new BookDAL();
            lb = new List<Book>();
            lb = boDAL.GetAllBook();
            ConvertBook_SBook(lb);
        }

        public void ConvertBook_SBook(List<Book> lb)
        {
            lsb = new List<SBook>();
            SBook sb = null;
            foreach (Book b in lb)
            {
                sb = new SBook(b);
                lsb.Add(sb);
            }

        }

        private void frmSearch_Resize(object sender, EventArgs e)
        {
            dgvBookSearch.Columns[1].Width = 390+dgvBookSearch.Size.Width-705;
        }

        private void dgvBookSearch_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            frmMain.IsBn = Convert.ToInt64(dgvBookSearch[0,e.RowIndex].Value);
            this.Close();
        }
    }
}
